from drf_haystack.viewsets import HaystackViewSet
from rest_framework.filters import OrderingFilter
from rest_framework.generics import ListAPIView
from rest_framework.response import Response
from rest_framework.views import APIView
from rest_framework.viewsets import ModelViewSet

from goods.models import SKU, GoodsCategory, Goods, GoodsSpecification, SpecificationOption, SKUImage
from goods.serializers import SKUSerializer, SKUIndexSerializer, BrandSerializer, SPUCateSimpleSerializer, \
    GoodsSerializer, GoodsSpecificationSerializer, SpecificationOptionSerializer, SKUImageSerializer, \
    SkuDefaultSerializer
from goods.utils import GoodsPage
from goods.models import Brand, GoodsChannel
from goods.serializers import GoodsChannelSerialiazer
from meiduo_mall.utils import PageNum


class GoodCategorieView(APIView):
    def get(self, request, pk):
        # 获取三级分类对象
        cat3 = GoodsCategory.objects.get(id=pk)
        # 获取二级分类对象和一级分类对象
        cat2 = cat3.parent
        cat1 = cat2.parent

        return Response({
            'cat1': cat1.name,
            'cat2': cat2.name,
            'cat3': cat3.name,
        })


class SKUListView(ListAPIView):
    """
    sku列表数据
    """
    serializer_class = SKUSerializer
    #  设置分页
    pagination_class = GoodsPage
    filter_backends = (OrderingFilter,)
    ordering_fields = ('create_time', 'price', 'sales')

    def get_queryset(self):
        category_id = self.kwargs['pk']
        return SKU.objects.filter(category_id=category_id, is_launched=True)


class SKUSearchViewSet(HaystackViewSet):
    """
    SKU搜索
    """
    index_models = [SKU]

    serializer_class = SKUIndexSerializer


class GoodsBrands(ModelViewSet):
    queryset = Brand.objects.all()
    serializer_class = BrandSerializer
    pagination_class = PageNum

    def simple(self, request, *args, **kwargs):
        queryset = Brand.objects.all()
        ser = BrandSerializer(queryset, many=True)
        return Response(ser.data)


class GoodsChannelViewSet(ModelViewSet):
    queryset = GoodsChannel.objects.all()
    serializer_class = GoodsChannelSerialiazer
    pagination_class = PageNum


class SPUCategoriesViewSet(ModelViewSet):
    queryset = GoodsCategory.objects.all()
    serializer_class = SPUCateSimpleSerializer
    pagination_class = PageNum

    def categories_detail(self, request, pk):
        parent_id = pk
        queryset = GoodsCategory.objects.all()
        query_set = queryset.filter(parent_id=parent_id) if parent_id else queryset.filter(parent_id=None)
        seri = SPUCateSimpleSerializer(query_set, many=True)
        return Response(seri.data)




class GoodsViewSet(ModelViewSet):
    queryset = Goods.objects.all()
    serializer_class = GoodsSerializer
    pagination_class = PageNum

    def simple(self, request, *args, **kwargs):
        queryset = Goods.objects.all()
        ser = GoodsSerializer(queryset, many=True)
        return Response(ser.data)


class GoodsSpecificationViewSet(ModelViewSet):
    queryset = GoodsSpecification.objects.all()
    serializer_class = GoodsSpecificationSerializer
    pagination_class = PageNum

    def simple(self, request, *args, **kwargs):
        queryset = GoodsSpecification.objects.all()
        ser = GoodsSpecificationSerializer(queryset, many=True)
        return Response(ser.data)


class SpecificationOptionViewSet(ModelViewSet):
    queryset = SpecificationOption.objects.all()
    serializer_class = SpecificationOptionSerializer
    pagination_class = PageNum


class GoodsImageViewSet(ModelViewSet):
    queryset = SKUImage.objects.all()
    serializer_class = SKUImageSerializer
    pagination_class = PageNum


class SkuViewSet(ModelViewSet):
    queryset = SKU.objects.all()
    serializer_class = SkuDefaultSerializer
    pagination_class = PageNum





